package LogServer.constants

import LogServer.utils.PropertiesUtil

/**
  * Created by root on 17-2-14.
  */

// 私有构造方法
/**
  * 必须实现Serializable,否则spark partition中报错
  * @param mongoUrl
  * @param mongoDbName
  * @param mongoUser
  * @param mongoPwd
  */
class PropertiesMongo(val mongoUrl:String,val mongoDbName: String,val mongoUser:String,val mongoPwd:String)
  extends Serializable {

}

// 伴生对象，与类共享名字，可以访问类的私有属性和方法
object PropertiesMongo{

  /**
    * get MongoDb url
    * @return MongoDb url
    */
  private def getMongoUrl(): String = {
    val propertiesUtil = new PropertiesUtil()
    val resource = propertiesUtil.loadResources("mongodb.properties")
    val path = propertiesUtil.getString("path",resource)
    val mongoProperties = propertiesUtil.loadProperties(path)
    val mongoUrl = propertiesUtil.getString("url",mongoProperties)
    mongoUrl
  }

  /**
    * get MongoDb dbName
    * @return MongoDb dbName
    */
  private def getMongoDbName(): String = {
    val propertiesUtil = new PropertiesUtil()
    val resource = propertiesUtil.loadResources("mongodb.properties")
    val path = propertiesUtil.getString("path",resource)
    val mongoProperties = propertiesUtil.loadProperties(path)
    val mongoDbName = propertiesUtil.getString("db",mongoProperties)
    mongoDbName
  }

  /**
    * get MongoDb user
    * @return MongoDb user
    */
  private def getMongoUser(): String = {
    val propertiesUtil = new PropertiesUtil()
    val resource = propertiesUtil.loadResources("mongodb.properties")
    val path = propertiesUtil.getString("path",resource)
    val mongoProperties = propertiesUtil.loadProperties(path)
    val mongoUser = propertiesUtil.getString("user",mongoProperties)
    mongoUser
  }

  /**
    * get MongoDb pwd
    * @return pwd
    */
  private def getMongoPwd(): String = {
    val propertiesUtil = new PropertiesUtil()
    val resource = propertiesUtil.loadResources("mongodb.properties")
    val path = propertiesUtil.getString("path",resource)
    val mongoProperties = propertiesUtil.loadProperties(path)
    val mongoPwd = propertiesUtil.getString("pwd",mongoProperties)
    mongoPwd
  }

  /**
    * 从Properties文件中获得所有Mongo连接属性
    * @return
    */
  def getAllProperties(): PropertiesMongo = {
    val mongoUrl = getMongoUrl()
    val mongoDbName = getMongoUser()
    val mongoUser = getMongoUser()
    val mongoPwd = getMongoPwd()
    val propertiesMongoObject = new PropertiesMongo(mongoUrl,mongoDbName,mongoUser,mongoPwd)
    propertiesMongoObject
  }

  def main(args: Array[String]): Unit = {
    println(getAllProperties())
  }
}
